package com.niiwoo.civet.trade.report.client.report.random;

import java.io.UnsupportedEncodingException;
import java.util.Random;

public class RandomTool {

    public String getRandomChar(int length) {            //生成随机字符串
        char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(36)]);
        }
        return buffer.toString();
    }

    public String getStringNum(int count){
        String value = String.valueOf(Math.random()).substring(2).replaceAll("0", "1");
        int start = Integer.parseInt(value.substring(value.length() - 1));
        value = value.substring(start, start + count);
        return value;
    }

    public int getIntNum(int count){
        String value = String.valueOf(Math.random()).substring(2).replaceAll("0", "1");
        int start = Integer.parseInt(value.substring(value.length() - 1));
        value = value.substring(start, start + count);
        return Integer.parseInt(value);
    }

    private String getRandomChar() {
        String str = "";
        int highCode;
        int lowCode;

        Random random = new Random();

        highCode = (176 + Math.abs(random.nextInt(5))); //B0 + 0~39(16~55) 一级汉字所占区
        lowCode = (161 + Math.abs(random.nextInt(5))); //A1 + 0~93 每区有94个汉字

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();

        try {
            str = new String(b, "GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public String getWords(int len) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0 ; i < len; i ++) {
            sb.append(getRandomChar());
        }
        return sb.toString();
    }

    public static void main(String args[]){
        RandomTool r = new RandomTool();
        System.out.println(r.getRandomChar(20));
    }
}
